﻿// 本地预览图片，只支持ie10+
function fileReader(file, obj, isPush) {
	var _this = file;
	var file = file.files[0];

	if (file) {
		var isImage = file.type.indexOf('image') !== -1;
		if (!isImage) {
			$(_this).removeClass('success').addClass('error');
			_this.outerHTML = $(_this).prop('outerHTML');
			if (isPush) {
				var preview_img = $('#preview_img');
				if (preview_img.length) preview_img.remove();
			}
			message('add', 'warning', '头像只能选择图片格式');
			return;
		}

		var reader = new FileReader();
		
		reader.readAsDataURL(file);

		reader.onload = function () {
			if (isPush) {
				if ($('#preview_img').length) {
					$('#preview_img').prop('src', this.result);
				} else {
					var oImg = new Image();
					oImg.id = 'preview_img';
					oImg.src = this.result;
					oImg.onload = function () {
						oImg.alt = oImg.title = '缩略图';
						$(obj).append(oImg);
						$(_this).removeClass('error').addClass('success');
					}
				}
			} else {
				var oImg = document.getElementById(obj);
				oImg.src = this.result;
			}
		}
	} else {
		if (isPush) {
			var preview_img = $('#preview_img');
			preview_img.attr('old-img') ? preview_img.prop('src', preview_img.attr('old-img')) : preview_img.remove();
		} else {
			var oImg = $('#' + obj);
			oImg.prop('src', oImg.attr('init-src'));
		}
	}
}

// RSA加密
function rsaEncrypt(data) {
	var encrypt = new JSEncrypt();
	encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----' +
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCx8fvyKnvyJnVWHrt4mmbv6/Dl' +
'Vo8IMwCgoj+7Fvw+RGAQgp0Rn6cy0i9jp/TlH0Jdcxaqm4Gh2pRHlRl+0Nsjv0YD' +
'ugYmczAaJ7yX1uqgJf5RVLSQqtrPPoozQ3XHhYysdHZZKFcJHE44VvAPhAEeA7NY' +
'U0QHCvoirp37l6tfmwIDAQAB' +
'-----END PUBLIC KEY-----');
	return encrypt.encrypt(data);
}